using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Kenly.DBFramework.ORMCodeHelper
{
    public static class CodeUtility
    {
        public static readonly string EmptyDataEntity = "EmptyDataEntity";

        public static string GetDataType(DataColumn column)
        {
            string dataType = "";
            switch (column.DataType.Name)
            {
                case "Int32": dataType = "int";
                    break;
                case "String": dataType = "string";
                    break;
                case "Boolean": dataType = "bool";
                    break;
                case "DateTime": dataType = "DateTime?";
                    break;
                default: dataType = column.DataType.Name;
                    break;
            }

            return dataType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pascalCamelString"></param>
        /// <returns></returns>
        public static string ToPascalCase(string pascalCamelString)
        {
            if (string.IsNullOrEmpty(pascalCamelString))
            {
                return CodeUtility.EmptyDataEntity;
            }

            if (pascalCamelString.Length <= 1)
            {
                return pascalCamelString;
            }

            if (pascalCamelString.Length == 2)
            {
                return ProcessTwoCharPart(pascalCamelString);
            }

            string varName = pascalCamelString.Substring(0, 1).ToUpper() + pascalCamelString.Substring(1);

            return varName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pascalCaseString"></param>
        /// <returns></returns>
        public static string PascalCaseToCamelCase(string pascalCaseString)
        {
            if (string.IsNullOrEmpty(pascalCaseString))
            {
                return CodeUtility.EmptyDataEntity;
            }

            if (pascalCaseString.Length<=1)
            {
                return pascalCaseString;
            }

            if (pascalCaseString.Length == 2)
            {
                return ProcessTwoCharPart(pascalCaseString);
            }

            string varName = "";
            if (pascalCaseString.Substring(0, 2).ToUpper() == pascalCaseString.Substring(0, 2))
            {
                varName = pascalCaseString;
            }
            else
            {
                varName = pascalCaseString.Substring(0, 1).ToLower() + pascalCaseString.Substring(1);
            }
            return varName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="blockCaseString"></param>
        /// <returns></returns>
        public static string BlockCaseToCamelCase(string blockCaseString)
        {
            string varName = BlockCaseToPascalCase(blockCaseString);
            if (varName.Length > 2)
            {
                varName = varName.Substring(0, 1).ToLower() + varName.Substring(1);
            }
            return varName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="blockCaseString"></param>
        /// <returns></returns>
        public static string BlockCaseToPascalCase(string blockCaseString)
        {
            if (string.IsNullOrEmpty(blockCaseString))
            {
                return CodeUtility.EmptyDataEntity;
            }
            string[] stringParts = blockCaseString.Split('_','.');
            if (stringParts==null)
            {
                return blockCaseString;
            }
            string varName = "";
            foreach (string stringPart in stringParts)
            {
                varName += ProcessStringPart(stringPart);
            }
            return varName;
        }

        private static string ProcessStringPart(string stringPart)
        {
            if (string.IsNullOrEmpty(stringPart) || stringPart.Length<=1)
            {
                return stringPart;
            }

            if (stringPart.Length == 2)
            {
                return ProcessTwoCharPart(stringPart);
            }

            return stringPart.Substring(0, 1).ToUpper() + stringPart.Substring(1).ToLower();
        }

        private static string ProcessTwoCharPart(string stringPart)
        {
            stringPart = stringPart.ToUpper();
            if (stringPart == "ID")
            {
                return "Id";
            }

            if (stringPart == "IS")
            {
                return "Is";
            }

            if (stringPart == "BY")
            {
                return "By";
            }

            return stringPart;
        }

        /// <summary>
        /// replaces \t with 4 spaces
        /// </summary>
        /// <returns></returns>
        public static string CreateTabSpaces()
        {
            return "    ";
        }

        public static string CreateTabSpaces(int count)
        {
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                str.Append(CreateTabSpaces());
            }
            return str.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullEntityName"></param>
        /// <returns></returns>
        public static string FilterOwner(string fullEntityName)
        {
             string owner;
             return FilterOwner(fullEntityName, out owner);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string FilterOwner(string fullEntityName,out string owner)
        {
            owner = "";
            if (string.IsNullOrEmpty(fullEntityName))
            {
                return fullEntityName;
            }

            string entityName = fullEntityName.Trim();
            int indexOfDot = fullEntityName.LastIndexOf('.');
            
            if (indexOfDot > 0 && fullEntityName.Length > (indexOfDot + 1))
            {
                entityName = fullEntityName.Substring(indexOfDot + 1).Trim();
                owner = fullEntityName.Substring(0,indexOfDot).Trim();
            }
            return entityName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static string ExtractTableName(string fullEntityName)
        {
            string tableName = fullEntityName;
            if (string.IsNullOrEmpty(tableName))
            {
                return "";
            }

            if (tableName.EndsWith("."))
            {
                tableName = tableName.Remove(tableName.Length - 1);
            }

            if (string.IsNullOrEmpty(tableName))
            {
                return "";
            }

            int index = tableName.LastIndexOf('.');
            if (index <= 0)
            {
                return tableName;
            }

            return tableName.Substring(index + 1);

        }
    }
}
